package com.justgame.project.service.art.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.justgame.project.cloud.common.constant.Result;
import com.justgame.project.cloud.common.util.MapBuilder;
import com.justgame.project.service.art.entity.ArtPageType;
import com.justgame.project.service.art.dto.ArtPageTypeDTO;
import com.justgame.project.service.art.wrapper.ArtPageTypeWrapper;
import com.justgame.project.service.art.controller.form.ArtPageTypeQueryForm;
import com.justgame.project.service.art.service.IArtPageTypeService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.Api;
import lombok.RequiredArgsConstructor;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.util.Assert;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 *
 * 文章类型表控制器
 *
 * @@tableName art_page_type
 * @author Zwb
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/art/page/type")
@Api("文章类型表控制器")
public class ArtPageTypeController {

    private final IArtPageTypeService artPageTypeService;

    @GetMapping("/detail")
    @ApiOperation(value = "通过主键id查询 文章类型表")
    public Result<?> query(@ApiParam("主键id") @NotNull(message = "必须提供需要查询的数据") Long id){
        return Result.ok(artPageTypeService.getById(id));
    }

    @PutMapping("/update")
    @RequiresAuthentication
    @ApiOperation(value = "通过主键id编辑 文章类型表")
    public Result<?> edit(@ApiParam("编辑表单") @RequestBody @Validated ArtPageTypeDTO dto){
        Assert.notNull(dto.getId(),"必须提供id");
        Assert.notNull(dto.getName(),"必须提供名称");
        Assert.notNull(dto.getOtherName(),"必须提供别名");
        Assert.notNull(dto.getDescription(),"必须提供描述");
        Assert.notNull(dto.getImageUrl(),"必须提供图片");
        Assert.notNull(dto.getStatus(),"必须提供状态");
        Assert.notNull(dto.getRemark(),"必须提供备注");
        ArtPageType artPageType = artPageTypeService.requireGetById(dto.getId(),"相关数据不存在");
        artPageType.setName(dto.getName());
        artPageType.setOtherName(dto.getOtherName());
        artPageType.setDescription(dto.getDescription());
        artPageType.setImageUrl(dto.getImageUrl());
        artPageType.setStatus(dto.getStatus());
        artPageType.setRemark(dto.getRemark());
        return Result.status(artPageTypeService.updateById(artPageType));
    }

    @PostMapping("/save")
    @RequiresAuthentication
    @ApiOperation(value = "新增 文章类型表")
    public Result<?> save(@ApiParam("新增表单") @RequestBody @Validated ArtPageTypeDTO dto){
        Assert.notNull(dto.getName(),"必须提供名称");
        Assert.notNull(dto.getOtherName(),"必须提供别名");
        Assert.notNull(dto.getDescription(),"必须提供描述");
        Assert.notNull(dto.getImageUrl(),"必须提供图片");
        Assert.notNull(dto.getStatus(),"必须提供状态");
        Assert.notNull(dto.getRemark(),"必须提供备注");
        ArtPageType artPageType = new ArtPageType();
        artPageType.setName(dto.getName());
        artPageType.setOtherName(dto.getOtherName());
        artPageType.setDescription(dto.getDescription());
        artPageType.setImageUrl(dto.getImageUrl());
        artPageType.setStatus(dto.getStatus());
        artPageType.setRemark(dto.getRemark());
        return Result.status(artPageTypeService.save(artPageType));
    }

    @DeleteMapping("/remove")
    @RequiresAuthentication
    @ApiOperation(value = "通过主键id删除 文章类型表")
    public Result<?> remove(@ApiParam("主键id") @NotNull(message = "必须提供需要删除的数据") Long id){
        return Result.status(artPageTypeService.requireRemoveById(id, "抱歉，您的账号权限不支持删除此数据"));
    }

    @GetMapping("/page")
    @ApiOperation(value = "分页查询")
    public Result<?> page(@ApiParam("查询表单") ArtPageTypeQueryForm queryForm){
        IPage<ArtPageTypeWrapper> page = artPageTypeService.selectJoinListPage(queryForm.buildPageQuery());
        return Result.ok(MapBuilder.builder()
                    .put("data",page.getRecords())
                    .put("rows",page.getTotal())
                .build());
    }

    @GetMapping("/list")
    @ApiOperation(value = "列表查询")
    public Result<?> list(@ApiParam("查询表单") ArtPageTypeQueryForm queryForm){
        List<ArtPageTypeWrapper> list = artPageTypeService.selectJoinList(queryForm.buildListQuery());
        return Result.ok(MapBuilder.builder()
                    .put("data",list)
                .build());
    }
}